package com.api.nanc.util;

import com.alibaba.fastjson.JSONObject;
import com.api.shihua.util.LogTool;
import weaver.conn.RecordSet;
import weaver.general.Util;

public class BaseUtil {
    private final static LogTool loghr = new LogTool("/log/hr/",false);
    public static JSONObject getRequestInfoById(String requstid){
        JSONObject json=new JSONObject();
        requstid= Util.null2String(requstid).trim();
        if("".equals(requstid)){
            return json;
        }
        RecordSet re=new RecordSet();
        String sql="select a.requestid,a.currentnodeid,c.nodename ,a.currentnodetype,a.workflowid,b.workflowname,b.formid from workflow_requestbase a left join workflow_base b on a.workflowid=b.id left join workflow_nodebase c on a.currentnodeid=c.id  where a.requestid='"+requstid+"'";
        re.execute(sql);
        while(re.next()){
            String requestid1=Util.null2String(re.getString("requestid")).trim();
            String currentnodeid=Util.null2String(re.getString("currentnodeid")).trim();
            String nodename=Util.null2String(re.getString("nodename"));
            String currentnodetype=Util.null2String(re.getString("currentnodetype")).trim();
            String workflowid=Util.null2String(re.getString("workflowid")).trim();
            String workflowname=Util.null2String(re.getString("workflowname"));
            String formid=Math.abs(Util.getIntValue(re.getString("formid"),1))+"" ;
            json.put("requestid",requestid1);
            json.put("currentnodeid",currentnodeid);
            json.put("nodename",nodename);
            json.put("currentnodetype",currentnodetype);
            json.put("workflowid",workflowid);
            json.put("workflowname",workflowname);
            json.put("formid",formid);
        }
        return json;
    }
    public static JSONObject getChildRequestInfoById(String requstid,String detailid){
        JSONObject json=new JSONObject();
        requstid= Util.null2String(requstid).trim();
        if("".equals(requstid)){
            return json;
        }
        RecordSet re=new RecordSet();
        String sql=" select a.requestid,a.currentnodeid,c.nodename ,a.currentnodetype,a.workflowid,b.workflowname,b.formid from workflow_requestbase a left join workflow_base b on a.workflowid=b.id left join workflow_nodebase c on a.currentnodeid=c.id  ,formtable_main_31 d " +
                   "  where  d.mainrequestid='"+requstid+"' and a.requestid=d.requestid and d.detailid="+detailid+
                   " union all select a.requestid,a.currentnodeid,c.nodename ,a.currentnodetype,a.workflowid,b.workflowname,b.formid from workflow_requestbase a left join workflow_base b on a.workflowid=b.id left join workflow_nodebase c on a.currentnodeid=c.id  ,formtable_main_27 d " +
                   "  where  d.mainrequestid='"+requstid+"' and a.requestid=d.requestid and d.detailid="+detailid;
       // String sql=" select a.requestid,a.currentnodeid,c.nodename ,a.currentnodetype,a.workflowid,b.workflowname,b.formid from workflow_requestbase a left join workflow_base b on a.workflowid=b.id left join workflow_nodebase c on a.currentnodeid=c.id  ,formtable_main_3 d " +
       //           "  where a.mainRequestId='"+requstid+"' and a.requestid=d.requestid and d.detailid="+detailid;
        loghr.writeLog(sql);
        re.execute(sql);
        while(re.next()){
            String requestid1=Util.null2String(re.getString("requestid")).trim();
            String currentnodeid=Util.null2String(re.getString("currentnodeid")).trim();
            String nodename=Util.null2String(re.getString("nodename"));
            String currentnodetype=Util.null2String(re.getString("currentnodetype")).trim();
            String workflowid=Util.null2String(re.getString("workflowid")).trim();
            String workflowname=Util.null2String(re.getString("workflowname"));
            String formid=Math.abs(Util.getIntValue(re.getString("formid"),1))+"" ;
            json.put("requestid",requestid1);
            json.put("currentnodeid",currentnodeid);
            json.put("nodename",nodename);
            json.put("currentnodetype",currentnodetype);
            json.put("workflowid",workflowid);
            json.put("workflowname",workflowname);
            json.put("formid",formid);
        }
        return json;
    }
    //获取当前节点未操作者
    public static String getNodeOpterV1(String requestid){
        RecordSet rs=new RecordSet();
        String returnStr="";
        if(requestid.indexOf("-")==-1) {
            //增加isremark字段，用来判断是否归档节点
            //rs.executeSql("select distinct userid,usertype,agenttype,agentorbyagentid from workflow_currentoperator where (isremark in ('0','1','5','7','8','9') or (isremark='4' and viewtype=0))  and requestid = " + requestid);
            rs.execute("select distinct userid,usertype,agenttype,agentorbyagentid,isremark from workflow_currentoperator where (isremark in ('0','1','5','7','8','9') or (isremark='4' and viewtype=0))  and requestid = " + requestid);

            while(rs.next()){
                if(returnStr.equals("")){
                    if(rs.getInt("usertype")==0){
                        //if(rs.getInt("agenttype")==2)
                        //	returnStr +=  rc.getResourcename(rs.getString("agentorbyagentid"))+"->"+rc.getResourcename(rs.getString("userid"));
                        //else
                        //	returnStr +=  rc.getResourcename(rs.getString("userid"));
                        if(rs.getInt("agenttype")==2){
                            returnStr +=  rs.getString("userid");
                            //判断是否被代理者,如果是，则不显示该记录
                        }else if(rs.getInt("agenttype")==1 && rs.getInt("isremark") == 4){
                            continue;
                        }else{
                            returnStr += rs.getString("userid");
                        }
                    }else{
                        returnStr +=  rs.getString("userid");
                    }
                }
                else{
                    if(rs.getInt("usertype")==0){
                        //if(rs.getInt("agenttype")==2)
                        //	returnStr +=  ","+rc.getResourcename(rs.getString("agentorbyagentid"))+"->"+rc.getResourcename(rs.getString("userid"));
                        //else
                        //	returnStr +=  ","+rc.getResourcename(rs.getString("userid"));
                        if(rs.getInt("agenttype")==2){
                            returnStr +=  ","+rs.getString("userid");
                            //判断是否被代理者,如果是，则不显示该记录
                        }else if(rs.getInt("agenttype")==1 && rs.getInt("isremark") == 4){
                            continue;
                        }else{
                            returnStr +=  ","+rs.getString("userid");
                        }
                    }else{
                        //TD11591(人力资源与客户同时存在时、加','处理)
                        returnStr +=  ","+rs.getString("userid");
                    }
                }
            }

            return returnStr;
        }else{
            rs.execute("select distinct userid from ofs_todo_data where requestid="+requestid+" and isremark='0' and islasttimes=1");
            while(rs.next()){
                returnStr += rs.getString("userid")+",";
            }
            if(returnStr.endsWith(",")){
                returnStr = returnStr.substring(0,returnStr.length()-1);
            }
            // out.print(returnStr);
            return returnStr;
        }
    }
}
